#ifndef QFROZENTREEWIDGET_H
#define QFROZENTREEWIDGET_H

#include <QLabel>
#include <QResizeEvent>
#include <QTreeWidget>
#include <QTreeWidgetItem>

//定位结构体，用来存储 展合、勾选 的item位置
struct structIndex
{
    int m_index;
    structIndex *m_pChildIndex;
    structIndex()
    {
        m_index = -1;
        m_pChildIndex = NULL;
    }
};

//当前类继承于QTreeWidget，this就是这个QTreeWidget
//同时本身再上层叠加了一个QTreeWidget保持浮动，实现树列冻结
class QFrozenTreeWidget : public QTreeWidget
{
    Q_OBJECT
private:
    int m_startColumn = 0, m_stopColumn = 0;
    QStringList m_back_lables; //后置表头
    QTreeWidget *m_backTreeWidget = this;

public:
    QTreeWidget *m_frontTreeWidget;
    QFrozenTreeWidget(QWidget *parent = 0);
    ~QFrozenTreeWidget();
public:
    void setColumnHeader(const QStringList &labels, int frozenColumn = 0, int startColumn = 0, int stopColumn = 1, int starttime = 0, int stoptime = 0);
    QTreeWidgetItem *createTopItem(QStringList lables, int starttime=0, int stoptime=0, QColor color=QColor("#fff"), int progress=100);
    QTreeWidgetItem *createChildItem(QTreeWidgetItem *parent, QStringList lables, int starttime=0, int stoptime=0, QColor color=QColor("#fff"),int progress=100);
    void setExecutionTime(QTreeWidgetItem *item, int starttime, int stoptime, QColor color, int progress);
    void setColumnHidden1(int column, bool hidden);
    void changedTimes(QTreeWidgetItem *item, int starttime, int stoptime, QColor color, int progress);

    void setHidden1(QTreeWidgetItem *itemb, bool hidden);

    void getBranchTextByFlags(QStringList &txt, int column, QTreeWidgetItemIterator::IteratorFlags flags = QTreeWidgetItemIterator::All);

    void deleteBranchByFlags(QTreeWidgetItemIterator::IteratorFlags flags);

    void showAllHidden();

    void deleteBranch(QTreeWidgetItem *bitem);

    void getBranchText(QStringList &txt, int column, QTreeWidgetItem *item);

    void changedText(QTreeWidgetItem *item, int column, const QString &txt);

    QString getHeadtxt(int column);

    bool setDateColumn(QTreeWidgetItem *item, int column, int &newstartdate, int &newstopdate);

    QTreeWidgetItem *getUpperItem(QTreeWidgetItem *item);
    QTreeWidgetItem *getLowerItem(QTreeWidgetItem *item);

    QTreeWidget *getUpperTree();

    QTreeWidget *getLowerTree();

    QLabel *m_l;
    void SetMomlable(QLabel *l);
public slots:
    void hSetValue(int val);

protected:
    void resizeEvent(QResizeEvent *event);
    QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
    void scrollTo(const QModelIndex &index, ScrollHint hint);
    //    void drawRow(QPainter *painter,
    //        const QStyleOptionViewItem &options,
    //        const QModelIndex &index) const;

private:
    void Init();
    void UpdateFrozenTreeGeometry();
    /*
     * 用来 从 固定列和本树 找对应的item
     * 比如 固定列某个item展开,要找到本树的对应位置item让其展开，实现同步
     * 下面是递归方法
     * 可能还有简单方法，比如：
     * 可以根据 鼠标位置 找到对应item,用itemAt(QPoint);
     * 可以根据 item 对应的QModelIndex 里row() 采用加法找到对应item，有兴趣的可以实现试试
     */
    structIndex *GetItemIndex(QTreeWidgetItem *targetItem, QTreeWidgetItem *sourceItem);
    QTreeWidgetItem *GetItemFromIndex(QTreeWidgetItem *item, structIndex *index);
    QTreeWidgetItem *GetItemFromItem(QTreeWidget *targetTree, QTreeWidgetItem *souceItem);
    /*
     * 下面实现同步勾选
     */
    void SetParentCheckState(QTreeWidgetItem *parentItem);
    void SetChildCheckState(QTreeWidgetItem *selfItem);
    void SetItemChecked(QTreeWidgetItem *item);
    int getFrontTreeWidth();
    int getDateColumnWidth();
private slots:
    void onCurrentItemChangedf(QTreeWidgetItem *current, QTreeWidgetItem *previous);
    void onCurrentItemChangedb(QTreeWidgetItem *current, QTreeWidgetItem *previous);
    void OnItemCollapsed(QTreeWidgetItem *item);
    void OnItemExpanded(QTreeWidgetItem *item);
    void OnItemChanged(QTreeWidgetItem *item, int column);
    void UpdateSectionWidthSlot(int logicalIndex, int, int newSize);

    void OnItemCollapsedf(QTreeWidgetItem *item);
    void OnItemExpandedf(QTreeWidgetItem *item);
    void UpdateSectionWidthSlotb(int logicalIndex, int, int newSize);
};

#endif // QFROZENTREEWIDGET_H
